package com.canbot.u05.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.bean.sales.ChoosePicBean;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by X230 on 2017/5/26.
 */

public class MerchandisePhotoItem extends RelativeLayout implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

        private OnPhotoItemCheckedListener photoItemCheckedListener;

        private OnItemClickListener onItemClickListener;

        private ImageView ivPhoto;

        private TextView textView;

        private CheckBox checkBox;

        private ChoosePicBean photo;

        private int position;

        private List<ChoosePicBean> list = new ArrayList<>();

//    private OnClickableListener l;


        private MerchandisePhotoItem(Context context) {
                super(context);
        }

        public MerchandisePhotoItem(Context context, OnPhotoItemCheckedListener listener) {
                this(context);
                LayoutInflater.from(context).inflate(R.layout.item_select_merchandise_pic, this, true);
                this.photoItemCheckedListener = listener;

                setOnClickListener(this);
                textView = (TextView) findViewById(R.id.tv);
                ivPhoto = (ImageView) findViewById(R.id.iv_map);
                checkBox = (CheckBox) findViewById(R.id.cb_select);
                checkBox.setOnCheckedChangeListener(this); // CheckBox选中状态改变监听器

        }

        @Override
        public void onClick(View view) {

                if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position);
                }
        }

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                // 让图片变暗或者变亮
                if (isChecked) {
                        setDrawingable();
//            ivPhoto.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                        list.add(photo);
                }
                else {
//            ivPhoto.clearColorFilter();
                        list.remove(photo);
                }
                photo.setChecked(isChecked);
//        if (count > 5){
//            checkBox.setClickable(false);
//            photoItemCheckedListener.onCheckedChanged(photo, compoundButton, isChecked,5); // 调用主界面回调函数
//        }else {
                photoItemCheckedListener.onCheckedChanged(photo, compoundButton, isChecked); // 调用主界面回调函数
//        }
//        l.onClickable(checkBox);
        }

        @Override
        public void setSelected(boolean selected) {
                if (photo == null) {
                        return;
                }
                checkBox.setChecked(selected);
        }

        public void setOnClickListener(OnItemClickListener l, int position) {
                this.onItemClickListener = l;
                this.position = position;
        }

        /**
         * 设置路径下的图片对应的名字
         */
        public void setImageName(final ChoosePicBean photo) {
                this.photo = photo;
                if(photo.getImagePath().contains("/uupic/")){
                        textView.setText(photo.getImagePath().replace("/uupic/",""));
                }
        }

        private void setDrawingable() {
                ivPhoto.setDrawingCacheEnabled(true);
                ivPhoto.buildDrawingCache();
        }

        /**
         * 图片Item选中事件监听器
         */
        public interface OnPhotoItemCheckedListener {

                void onCheckedChanged(ChoosePicBean photoModel, CompoundButton buttonView, boolean isChecked);
        }

        /**
         * 图片点击事件
         */
        public interface OnItemClickListener {

                void onItemClick(int position);
        }


//    public interface OnClickableListener {
//        void onClickable(CheckBox checkBox);
//    }
//
//    public void setClick(OnClickableListener l){
//       this.l = l;
//    }
}
